package com.winit.openapi.sms.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.common.spi.SPIException;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.ValidateUtil;
import com.winit.sms.spi.common.vo.PageVo;

/**
 * 工具类
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * keke.xu 	1.0  		2016年1月11日 	Created
 * 
 * </pre>
 * @since 1.
 */
public class SmsUtils {

    private static Logger logger = LoggerFactory.getLogger(SmsUtils.class);
    
    /**
     * 线上充值单成功状态
     */
    public static final String OLRECHARGE_STATUS_SUSS = "ORC".intern();
    
    /**
     * 产品线isp
     */
    public static final String ISP_PRODUCT_LINE = "ISP".intern();
    
    /**
     * 状态值Y
     */
    public static final String STATUS_Y = "Y".intern();
    
    /**
     * 状态值N
     */
    public static final String STATUS_N = "N".intern();
    
    /**
     * 两周的天数
     */
    public static final int DEL_TWO_WEEK_DAYS = -14;

    /**
     * 
     * 转jsonObject
     * @param obj
     * @return
     */
    public static JSONObject toJsonObject(Object obj) {
        if (obj == null) return null;
        if (obj instanceof JSONObject) {
            return (JSONObject) obj;
        } else {
            throw new ApiException(ErrorCode.JSON_FORMAT_ERROR, "data");
        }
    }

    /**
     * JSON转java对象
     * 
     * @param obj
     * @param type
     * @return
     */
    public static <E> E toJavaObject(Object obj, Class<E> type) {
        if (obj == null) return null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            try {
                return JSONObject.toJavaObject(json, type);
            } catch (SPIException e) {
                throw new ApiException(ErrorCode.JSON_TO_JAVAOBJECT_EROOR, "data");
            } catch (Exception e1) {
                logger.error(e1.getMessage());
                throw new ApiException(ErrorCode.JSON_TO_JAVAOBJECT_EROOR, "data");
            }
        } else {
            throw new ApiException(ErrorCode.JSON_FORMAT_ERROR, "data");
        }
    }

    /**
     * 返回分页数据
     * 
     * @param pagelist
     * @return
     */
    public static Map<String, Object> setPageData(Page<?> pagelist) {
        if (pagelist == null) {
            return null;
        }
        long total = pagelist.total;
        if (total < 1) {
            return null;
        }

        Map<String, Object> pageVo = new HashMap<String, Object>();
        pageVo.put("pageNo", pagelist.getPageable().getPageNumber());
        pageVo.put("pageSize", pagelist.getPageable().getPageSize());
        pageVo.put("totalCount", total);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", pagelist.content);
        data.put("pageVo", pageVo);
        return data;
    }

    /**
     * 设置分页参数
     * 
     * @param object
     * @return
     */
    public static PageVo SetPageParams(Object object) {
        if (object == null) return null;
        if (object instanceof JSONObject) {
            JSONObject json = (JSONObject) object;
            if (json.containsKey("pageVo")) {
                Object obj = json.get("pageVo");
                if (obj instanceof JSONObject) {
                    return SetPageVo(obj);
                } else {
                    throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "pageVo");
                }
            }
        } else {
            throw new ApiException(ErrorCode.JSON_FORMAT_ERROR, "data");
        }
        return null;
    }

    private static PageVo SetPageVo(Object obj) {
        PageVo pageVo = new PageVo();
        JSONObject pageParams = (JSONObject) obj;
        ValidateUtil.validInteger(pageParams, "pageNo");
        ValidateUtil.validInteger(pageParams, "pageSize");
        if (pageParams.containsKey("pageNo")) {
            int pageNo = pageParams.getInteger("pageNo");
            if (pageNo < 1) {
                throw new ApiException(ErrorCode.FIELD_ERROR, "pageNo");
            }
            pageVo.setPageNo(pageNo);
        }
        if (pageParams.containsKey("pageSize")) {
            int pageSize = pageParams.getInteger("pageSize");
            if (pageSize < 1) {
                throw new ApiException(ErrorCode.FIELD_ERROR, "pageSize");
            }
            pageVo.setPageSize(pageSize);
        }
        return pageVo;
    }

    /**
     * JSON List解析方法
     * @param obj json对象
     * @param type class文件
     * @param splat 解析的节点
     * @return
     */
    public static <E> List<E> getObjectList(Object obj, Class<E> type, String splat) {
        JSONObject jsonObj = JSON.parseObject(obj.toString());
        JSONArray result = jsonObj.getJSONArray(splat);
        List<E> links = JSON.parseArray(result.toJSONString(), type);
        return links;
    }
}
